// subPackages3/pages/gameManage/gameTakePrizeAddress/gameTakePrizeAddress.js
var regeneratorRuntime = require ("../../../../lib/js/runtime-module.js")
import cardConfig from '../../../../config.js'
import Serv from './gameTakePrizeAddressServ.js';
import util from '../../../../utils/util'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    loadingHidden:false,
    tipText:"",//提示语
    mobilePhone: "",//客户手机号
    gameActivityAwardId: "",//奖项id
    gamePrizeProductList:[],//兑奖地址列表
    relationMap: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userData = wx.getStorageSync('userData');
    this.setData({
      mobilePhone: userData.mobile,
      gameActivityAwardId: options.gameActivityAwardId
    });
    this.getRedeemProductList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  async getRedeemProductList(){
    let { mobilePhone, gameActivityAwardId,  gamePrizeProductList} = this.data;
    let pamars = {
      mobilePhone: mobilePhone,
      gameActivityAwardId: gameActivityAwardId
    };
    let { success, result, timestamp } = await Serv.getRedeemProductList(pamars);
    this.setData({
      loadingHidden: true
    });
    if(success){
      
      if (result && result.length>0){
        if (result.length == 1){
          this.setData({
            tipText:"请前往项目进行兑奖"
          });
        }else{
          this.setData({
            tipText: "请选择其中一个项目前往兑奖"
          });
        }
        gamePrizeProductList = [];
        for (let i = 0; i < result.length;i++){
          if (result[i].elabels){
            result[i].elabels = JSON.parse(result[i].elabels).slice(0, 2);
          }
          gamePrizeProductList.push(result[i]);
        }
        this.setData({
          gamePrizeProductList: gamePrizeProductList
        });
      }
    }
  },
  callphone(e) {
    console.log(e)
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  massage: function (e, type) {
    let eId1 = e.currentTarget.dataset.eid;
    app.subscribeMessage(() => {
      wx.navigateTo({
        url: "/subPackages/pages/chat_detail/chat_detail?eId=" + eId1
      });
    }, cardConfig.config.messageTemplateId);
    wx.setStorageSync("has_readmsg_" + eId1, true);
  },
  toCard(e) {
    var ctx = this;
    var eId = e.currentTarget.dataset.eid;
    if (!ctx.data.relationMap[eId]) { //没有关系才进行绑定
      util.addRelation(eId, wx.getStorageSync('cusmallToken'), '置业神器');
    }
    wx.setStorageSync("eId", eId); //员工id
    // wx.switchTab({
    //   url: '/pages/mycard/card?eId=' + eId
    // });
    wx.navigateTo({
      url: '/pages/mycard/card?eId=' + eId
    })

  },
  //打开微信小程序自带定位功能
  toMap(e) {
    var { latitude, longitude, mapaddress} = e.currentTarget.dataset;
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      name: mapaddress || ""
    })
  },
})